/**
 * @file:          MT68xx.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.09
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.09,18:31:37
 */
#ifndef _MT68XX_H_
#define _MT68XX_H_

/* Include Files **************************************************************/
#include "Datatype.h"
// #include "STC32G8K64.h"
#include "ch32v30x_module.h"
#include "encoder_cal_table.h"
#include "EncoderCalibrator.h"

/* Macro Definition ***********************************************************/

/* Data Type Define ***********************************************************/
enum EncoderCalibration_Status
{
    EncoderCalibrationStatus_NoStart = 0x0,
    EncoderCalibrationStatus_Running = 0x4,
    EncoderCalibrationStatus_Failed = 0x8,
    EncoderCalibrationStatus_Success = 0xc,
};

typedef struct MT68xx
{
    /* public data */
    Mcu *mcu;
    // EncoderCalibrator calibrator;

    uint32 parma_encoder_bit;
    int32 param_encoder_max_pulse;
    int32 param_direction;
    uint16 param_single_turn_mode;

    uint16 param_fake_encoder_en;
    float32 param_fake_encoder_step;

    float32 fake_encoder;

    int32 encoder_raw;
    int32 encoder_used;
    int32 encoder_pulse_single_turn;
    int32 encoder_pulse_single_turn_last;
    int32 encoder_pulse_multi_turns;
    int32 encoder_turns;
    uint16 varify_pass;

    union
    {
        uint8_t all;
        struct
        {
            uint8_t progress : 4;
            uint8_t status : 4;
        } bit;
    } calibration_result;

    void *raw_data;

    /* private data */

    /* public method */
    void (*ConfigParam)(struct MT68xx *self);
    uint8 (*Init)(struct MT68xx *self);
    int32 (*SingleToMultiTurns)(struct MT68xx *self);
    int32 (*GetEncoder)(struct MT68xx *self);
    int32 (*EncoderRawRead)(struct MT68xx *self);
    int32 (*GetEncoderCalibrated)(struct MT68xx *self);
    int32 (*GetEncoderSingleturn)(struct MT68xx *self);
    uint8_t (*GetEncoderCalibrationStatus)(struct MT68xx *self);
    uint16_t (*WriteReg)(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
    uint16_t (*ReadReg)(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
    uint16_t (*SaveIntoEEPROM)(struct MT68xx *self);

    /* private method */

} MT68xx;

/* Variable Declare ***********************************************************/

/* Inline Function Define *****************************************************/

/* Function Declare ***********************************************************/

#endif